const axios = require('axios')
const qs = require('querystring')
const result = require('./result.js')
// 拼接tokenURL地址
let param = qs.stringify({
	grant_type: 'client_credential',
	appid: 'wxbd736cd3bafa911c',
	secret: 'd2dd70b29191c50d040998105f262a59'
})
// 获取token认证的url
let url = 'https://api.weixin.qq.com/cgi-bin/token?' + param
// 云环境ID
let env = 'cloud1-4gfhls3pb83e8f3c'
// 数据库插入记录url
let Addurl = 'https://api.weixin.qq.com/tcb/databaseadd?access_token='
// 数据库删除记录url
let Deleteurl = 'https://api.weixin.qq.com/tcb/databasedelete?access_token='
// 数据库修改记录url
let Updateurl = 'https://api.weixin.qq.com/tcb/databaseupdate?access_token='
// 数据库查询记录url
let Queryurl = 'https://api.weixin.qq.com/tcb/databasequery?access_token='
// 云存储上传文件url
let UploadFileUrl = 'https://api.weixin.qq.com/tcb/uploadfile?access_token='
// 云存储下载文url
let DownloadFileUrl = 'https://api.weixin.qq.com/tcb/batchdownloadfile?access_token='
// 云存储删除文url
let DeleteFileUrl = 'https://api.weixin.qq.com/tcb/batchdeletefile?access_token='
// 获取token地址，进行增删改查操作
class getToken {
	constructor() { }
	// 获取token
	async gettoken() {
		try {
			let token = await axios.get(url)
			if (token.status === 200) {
				return token.data.access_token
			} else {
				throw '获取token错误'
				// 出现throw就会进入到catch里面，并且throw给的值会在catch的参数里
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
	// 调用云开发http api接口
	async posteve(dataurl, query) {
		try {
			let token = await this.gettoken()
			let data = await axios.post(dataurl + token, {
				env,
				query
			})
			// console.log(data)
			if (data.data.errcode === 0) {
				return data.data
			} else {
				throw '请求出错'
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
	// 调用云存储上传第一次
	async posteve1(dataurl, path) {
		try {
			let token = await this.gettoken()
			let data = await axios.post(dataurl + token, {
				env,
				path
			})
			// console.log(data)
			if (data.data.errcode === 0) {
				return data.data
			} else {
				throw '请求出错'
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
	// 调用云存储上传第二次
	async posteve2(url, formData) {
		try {
			let data = await axios.post(url, formData, {
				Headers: {
					...formData.getHeaders()
				}
			})
			// console.log('第二次调用云存储接口',data)
			if (data.status === 204) {
				return true;
			} else {
				throw '请求出错'
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
	// 调用云存储删除文件接口
	async posteve3(dataurl, fileid_list) {
		try {
			let token = await this.gettoken()
			let data = await axios.post(dataurl + token, {
				env,
				fileid_list
			})
			// console.log('云存储删除文件返回的数据',data)
			if (data.status === 200) {
				return data.data
			} else {
				throw '请求出错'
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
	// 调用下载云存储文件接口
	async posteve4(dataurl, file_list) {
		try {
			let token = await this.gettoken()
			let data = await axios.post(dataurl + token, {
				env,
				file_list
			})
			// console.log('云存储下载文件返回的数据',data)
			if (data.status === 200) {
				return data.data
			} else {
				throw '请求出错'
			}
		} catch (e) {
			throw new result(e, 500)
		}
	}
}

// 导出
module.exports = {
	getToken,
	Addurl,
	Deleteurl,
	Updateurl,
	Queryurl,
	UploadFileUrl,
	DownloadFileUrl,
	DeleteFileUrl
}